<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Rbac;

use App\Controller\Admin\AdminController;
use App\Model\RbacPermissionHasSource;
use App\Repositories\AdminRepository;
use App\Repositories\RbacPermissionRepository;
use App\Repositories\RbacRoleRepository;
use App\Request\Rbac\StoreRoleRequest;
use App\Request\StoreAdminRequest;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;

class PermissionController extends AdminController{
    public function all(){
        $search['id']=[
            'field' => 'id',
            'value' => '1',
            'method' => '!='
        ];
        $datas = AdminRepository::getInstance()->getList('*',$search,false);
        return $this->response->success($datas);
    }

    public function list(){
        $rows = $this->request->input('rows',10);
        $search = [];
        $datas = RbacPermissionRepository::getInstance()->getList('*',$search,['limit'=>$rows]);
        return $this->response->success($datas);
    }

    public function store(StoreRoleRequest $request){
        $params = $request->all();
        $menu_ids = explode(',',$params['menu_ids']);
        DB::beginTransaction();
        try{
            $result = RbacPermissionRepository::getInstance()->store($params);
            if(!$result['success']){
                throw new \Exception('权限保存失败');
            }
            $permission = $result['data'];
            $select_ids = RbacPermissionHasSource::where('permission_id',$permission->id)->where('source_type','menu')->get()->pluck('source_id')->toArray();
            $delete = array_merge(array_diff($select_ids,$menu_ids));
            $add = array_merge(array_diff($menu_ids,$select_ids));
            if(!empty($delete)){
                RbacPermissionHasSource::where('permission_id',$permission->id)
                    ->whereIn('source_id',$delete)->where('source_type','menu')->delete();
            }
            if(!empty($add)){
                foreach ($add as $value){
                    $model = new RbacPermissionHasSource();
                    $model->permission_id = $permission->id;
                    $model->source_id= $value;
                    $model->source_type='menu';
                    if(!$model->save()){
                        throw new \Exception('权限资源保存失败，请稍后重试。');
                    }
                }
            }
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(100415,$e->getMessage());
        }
        return $this->response->success([
            'id' => $permission->id
        ]);
    }

    public function detail(){
        $id = $this->request->input('id',0);
        $menu_ids = RbacPermissionHasSource::query()->where('permission_id',$id)->where('source_type','menu')->get()->pluck('source_id')->toArray();
        return $this->response->success([
            'menu_ids' => $menu_ids
        ]);
    }

    public function delete(){

    }

    public function changeField()
    {
        $field = $this->request->input('field');
        $value = $this->request->input('value',false);
        $id = $this->request->input('id');
        $update =[
            $field=>$value=='true'?1:0
        ];
        $where=[
            [
                'field'=>'id',
                'value'=>$id
            ]
        ];
        $return = AdminRepository::getInstance()->update($update,$where);
        return $this->response->success([]);
    }
}